﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;

namespace HMS.Web.Helpers
{
    public class GenericFunction
    {

        public bool SendEmail(string from, string subject, string body, string to, string ClinicCode)
        {
            try
            {
                MailMessage mail = new MailMessage();
                if (to == null || to == String.Empty)
                {
                    if (ClinicCode == "")
                        mail.To.Add(ConfigurationManager.AppSettings["ContactEmail"]);
                    else
                        mail.To.Add(ConfigurationManager.AppSettings[ClinicCode.ToUpper() + "ContactEmail"]);
                }
                else
                    mail.To.Add(to);
                if (from == null || from == String.Empty)
                {
                    if (ClinicCode == "")
                        mail.From = new MailAddress(ConfigurationManager.AppSettings["EmailUsername"]);
                    else
                        mail.From = new MailAddress(ConfigurationManager.AppSettings[ClinicCode.ToUpper() + "EmailUsername"]);
                }
                else
                    mail.From = new MailAddress(from);
                mail.Subject = subject;
                string Body = body;
                mail.Body = Body;
                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                if (ClinicCode == "")
                    smtp.Host = ConfigurationManager.AppSettings["EmailServer"];
                else
                    smtp.Host = ConfigurationManager.AppSettings[ClinicCode.ToUpper() + "EmailServer"];
                if (ClinicCode == "")
                    smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["EmailPort"]);
                else
                    smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings[ClinicCode.ToUpper() + "EmailPort"]);
                smtp.UseDefaultCredentials = false;
                if (ClinicCode == "")
                    smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["EmailUsername"], ConfigurationManager.AppSettings["EmailPwd"]);
                else
                    smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings[ClinicCode.ToUpper() + "EmailUsername"], ConfigurationManager.AppSettings[ClinicCode.ToUpper() + "EmailPwd"]);
                smtp.EnableSsl = false;
                smtp.Send(mail);
                return true;
            }
            catch (Exception ex)
            {

                Logger.Error("Email not sent to : " + to);
                Logger.Error("Time  : " + DateTime.Now);
                Logger.Error("Exception Message : " + ex.Message);
                Logger.Error("Exception inner Exception : " + ex.InnerException);
                return false;
            }
        }

        public bool SendSMS(string number, string message, string ClinicCode)
        {
            double n;
            if (number != null && number.Contains('('))
            {
                number = number.Replace("(", string.Empty);
            }
            if (number != null && number.Contains(')'))
            {
                number = number.Replace(")", string.Empty);
            }
            message = message.Replace("%3Cbr/%3E", "%0A");
            if (double.TryParse(number, out n))
            {
                try
                {
                    string userName = ConfigurationManager.AppSettings[ClinicCode + "SMSAccountUserName"];
                    string password = ConfigurationManager.AppSettings[ClinicCode + "SMSAccountPassword"];
                    string senderId = ConfigurationManager.AppSettings[ClinicCode + "SMSSenderId"];
                    string URL = "http://bhashsms.com/api/sendmsg.php?user=" + userName + "&pass=" + password + "&sender=" + senderId + "&phone=" + number + "&text=" + message + "&priority=ndnd&stype=normal"; ;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                    request.KeepAlive = false;
                    //request.ProtocolVersion = HttpVersion.Version10;
                    request.Method = "POST";
                    request.Timeout = 30000;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode.GetHashCode() == 200)
                        return true;
                    else
                        return false;
                }
                catch (Exception Ex)
                {
                    Logger.Error("Sms not sent to : " + number);
                    Logger.Error("Time  : " + DateTime.Now);
                    Logger.Error("Exception Message : " + Ex.Message);
                    Logger.Error("Exception inner Exception : " + Ex.InnerException);
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public string GenerateEncodedMessage(string message)
        {
            return Uri.EscapeUriString(message);
        }
    }
}